﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        List<clsSprite> list = null;
        clsSprite mySprite1;
        clsSprite mySprite2;
        clsSprite mySprite3;
        clsSprite mySprite4;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = 1028;
            graphics.PreferredBackBufferHeight = 700;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            list = new List<clsSprite>(); 
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            mySprite1 = new clsSprite(Content.Load<Texture2D>("Image\\ahmad hania logo"),
                new Vector2(0f, 0f), new Vector2(128f, 128f),
            graphics.PreferredBackBufferWidth,
            graphics.PreferredBackBufferHeight);
            mySprite2 = new clsSprite(Content.Load<Texture2D>("Image\\airplane"), new
            Vector2(20f, 18f), new Vector2(128f, 128f),
            graphics.PreferredBackBufferWidth,
            graphics.PreferredBackBufferHeight);
            mySprite3 = new clsSprite(Content.Load<Texture2D>("Image\\align bottom"), new
            Vector2(50f, 60f), new Vector2(128f, 128f),
            graphics.PreferredBackBufferWidth,
            graphics.PreferredBackBufferHeight);
            mySprite4 = new clsSprite(Content.Load<Texture2D>("Image\\align hor center"), new
           Vector2(218f, 118f), new Vector2(128f, 128f),
           graphics.PreferredBackBufferWidth,
           graphics.PreferredBackBufferHeight);
            // Create a SpriteBatch to render the sprite
            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
            // set the speed the sprites will move
            mySprite1.velocity = new Vector2(5, 5);
            mySprite3.velocity = new Vector2(3, -3);
            mySprite4.velocity = new Vector2(5, 5);
            list.Add(mySprite1);
            list.Add(mySprite2);
            list.Add(mySprite3);
            list.Add(mySprite4);


            // TODO: use this.Content to load your game content here
        }
        protected void checkKeyboard()
        {
            KeyboardState keyboardState = Keyboard.GetState();
            if (keyboardState.IsKeyDown(Keys.Up))
                mySprite2.position += new Vector2(0, -5);
            if (keyboardState.IsKeyDown(Keys.Down))
                mySprite2.position += new Vector2(0, 5);
            if (keyboardState.IsKeyDown(Keys.Left))
                mySprite2.position += new Vector2(-5, 0);
            if (keyboardState.IsKeyDown(Keys.Right))
                mySprite2.position += new Vector2(5, 0);
            foreach (clsSprite item in list)
            {
                if (item.Collides(mySprite2))
                {
                    item.velocity *= -1;
                }
            }
           
        }
        protected void checkMouse()
        {
            // Hàm lenh se giúp sprite2 di chuyen đên ta đo ca mouse
            //if (mySprite2.position.X < Mouse.GetState().X)
            //    mySprite2.position += new Vector2(5, 0);
            //if (mySprite2.position.X > Mouse.GetState().X)
            //    mySprite2.position += new Vector2(-5, 0);
            //if (mySprite2.position.Y < Mouse.GetState().Y)
            //    mySprite2.position += new Vector2(0, 5);
            //if (mySprite2.position.Y > Mouse.GetState().Y)
            //    mySprite2.position += new Vector2(0, -5);
            //if (.Collides(mySprite2))
            //{
            //    mySprite1.velocity *= -1;
            //}
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            
            mySprite1.texture.Dispose();
            mySprite2.texture.Dispose();
            mySprite3.texture.Dispose();
            mySprite4.texture.Dispose();
            spriteBatch.Dispose();
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            checkKeyboard();
            mySprite1.Move();
            mySprite2.Move();
            mySprite3.Move();
            mySprite4.Move();
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
            
            mySprite1.Draw(spriteBatch);
            mySprite2.Draw(spriteBatch);
            mySprite3.Draw(spriteBatch);
            mySprite4.Draw(spriteBatch);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
